package com.group6.exam.teacher.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.group6.exam.common.entity.PageModel;
import com.group6.exam.common.entity.PaperEntity;
import com.group6.exam.common.util.CollectionUtil;
import com.group6.exam.teacher.dao.IPageDao;
import com.group6.exam.teacher.dao.PageDaoImpl;

public class pageServiceImpl implements IPageService {

	IPageDao pageDao = new PageDaoImpl();

	/**
	 * 查询试卷列表
	 */
	public PageModel maintain(PageModel pageModel, PaperEntity paper) {
		// 查询总条数
		int totalRecords = pageDao.getTotalRecords(paper);
		int pageSize = pageModel.getPageSize();
		// 获取总页数
		PageModel page = new PageModel(totalRecords, pageSize);
		// 设置当前页
		page.setCurrentPage(pageModel.getCurrentPage());
		List<Map<String, Object>> mapList = new ArrayList<>();
		List<Map<String, Object>> List = pageDao.maintain(pageModel, paper);
		for (Map<String, Object> map : List) {
			String subject_id = map.get("subject_id") + "";
			String subjectName = pageDao.getSubjectName(subject_id);
			map.put("subject_name", subjectName);
			mapList.add(map);
		}
		page.setList(mapList);
		return page;
	}

	/**
	 * 删除试卷
	 */
	public int deletePaper(String paperId) {
		return pageDao.deletePaper(paperId);
	}

	/**
	 * 修改前初始化
	 */
	public Map<String, Object> initUpdate(String paperId) {
		return pageDao.initUpdate(paperId);
	}

	@Override
	public int update(PaperEntity paper) {
		return pageDao.update(paper);
	}

	/**
	 * 初始化科目名称
	 */
	public List<Map<String, Object>> initType() {
		return pageDao.initType();
	}

	/**
	 * 初始化用户名
	 */
	public List<Map<String, Object>> initUserName() {
		return pageDao.initUserName();
	}

	/**
	 * 添加试卷信息
	 */
	public int addPaper(PaperEntity paper) {
		// TODO Auto-generated method stub
		return pageDao.addPaper(paper);
	}

	/**
	 * 条件查询（得到科目ID）
	 */
	public String getId(String subjectName) {
		return pageDao.getId(subjectName);
	}

	/**
	 * 得到试卷信息
	 */
	public Map<String, Object> getPaperById(String paperId) {
		List<Map<String, Object>> list = pageDao.getPaperById(paperId);
		return CollectionUtil.isEmpty(list) ? list.get(0) : null;
	}

	/**
	 * 初始化题
	 */
	public List<Map<String, Object>> getQuestionsByTypeId(String typeId,
			String number) {

		return pageDao.getQuestionsByTypeId(typeId, number);
	}

}
